<?php


namespace app\admin\controller;


use app\api\controller\Task;
use app\common\service\Bs;
use app\common\service\Timer;

class Admin extends Base
{
    public function index()
    {
        $page = input('page', 1);
        $param = input();
        $data = model('Admin')->list_data($param, $page, 10);
        $this->assign('param', $param);
        $this->assign('data', $data);
        $this->assign('auth_role', model('AuthRole')->getAll());
        return $this->fetch();
    }

    function form()
    {
        $id = input('id', 0);
        if ($id) {
            $this->assign('data', model('Admin')->fetch_data($id));
        }
        $this->assign('auth_role', model('AuthRole')->getAll());
        return $this->fetch();
    }

    public function action_data()
    {
        if (!request()->isPost()) {
            data_return('非法操作', [], -1);
        }
        $param = input('post.');
        if (!empty($param['type']) && $param['type'] == 'setStatus') {
            $res = model('Admin')->update_data($param);
        } elseif (!empty($param['type']) && $param['type'] == 'del') {
            $res = model('Admin')->del_data($param['id']);
        } else {
            if (!empty($param['id'])) {
                if(!empty($param['password'])){
                    $param['password']=sha1($param['password']);
                }else{
                    unset($param['password']);
                }
                $res = model('Admin')->update_data($param);
            } else {
                unset($param['id']);
                $param['password']=sha1($param['password']);
                $res = model('Admin')->create_data($param);
            }
        }
        data_return('操作成功', $res);
    }

    //手动获取信息
    public function get_info()
    {
        if (!request()->isPost()) {
            data_return('非法操作', [], -1);
        }
        $id = request()->post('id');
        $uriInfo = model('Admin')->fetch_data($id);
        $info = Bs::get_cert_info($uriInfo['domain']);
        if ($info['code'] <> 0) {
            data_return_error($info['msg']);
        }
        Timer::check_domain(null, $id);
        data_return('获取完成');
    }
}